package com.woniu.consumer;

import com.woniu.ComplainFeignClient;
import com.woniu.pojo.Complaint;
import com.woniu.pojo.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ComplainConsumer {

    @Autowired
    private ComplainFeignClient complainFeignClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    //收到用户对门店的投诉
    @RabbitListener(queues = "complain-shop-queue")
    public void getMessage(@Payload Message message){
        System.out.println("shopId = " + message.getShopId());
        System.out.println("userId = " + message.getUserId());
        System.out.println("content = " + message.getContent() );
    }

    //死信队列判断投诉是否过期
    @RabbitListener(queues = "queue-dead")
    public void handle(@Payload Integer complaintId){
        System.out.println("死信队列执行了");
        Complaint complaint= new Complaint();
        complaint.setId(complaintId);
        complaint.setType(1);
        complaint.setStatus(1);
        complainFeignClient.update(complaint);
        Message message = new Message();
        message.setType(1);
        message.setContent("多次沟通，由后台协调");
        message.setStatus(0);
        message.setUserId(complaint.getUserId());
        message.setShopId(0);
        rabbitTemplate.convertAndSend("complain-fanout","",message);
    }

    //广播通知门店上传后台
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("message-shop"),
            exchange = @Exchange(value = "complain-fanout",type = ExchangeTypes.FANOUT),
            key = "message"))
    public void getShopMessage(@Payload Message message){
        System.out.printf("门市收到的message = " + message);
    }

    //同时通知后台
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("message-back"),
            exchange = @Exchange(value = "complain-fanout",type = ExchangeTypes.FANOUT),
            key = "message"))
    public void getBackMessage(@Payload Message message){
        System.out.printf("后台收到的message = " + message);
    }

    //同时通知后台
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("message-shop-queue"),
            exchange = @Exchange(value = "message-exchange",type = ExchangeTypes.FANOUT),
            key = "message"))
    public void getMessage(String message){
        System.out.printf("门店收到的message = " + message);
    }
}
